//
//  NSObject+zxh.m
//  LuxuryCarUser55like
//
//  Created by 55like on 23/08/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "NSObject+add.h"

#import "objc/runtime.h"

static char somethingAdd;
@implementation NSObject (add)



-(id)getSafeObjWithkey:(NSString*)key{
    
    if ([self isKindOfClass:[NSString class]]) {
        return nil;
    }
    
    id dic=self;
    if (![self isKindOfClass:[NSDictionary class]]) {
        
        
        if (![self respondsToSelector:NSSelectorFromString(key)]) {
            return [self getAddValueForKey:key];
        }
        
        return   [self valueForKeyPath:key];
        
        
    }
    
    NSDictionary*dict=dic;
    id value=  [dict objectForKey:key];
    //    id value = [self objectForKey:aKey];
    if (!value||value==[NSNull null]) {
        
        
        return nil;
        
        
    }else{
        if ([value isKindOfClass:[NSNumber class]]) {
            return [NSString stringWithFormat:@"%@",value];
        }
        else if([value isKindOfClass:[NSString class]]){
            if ([value isEqualToString:@""] || [value isEqualToString:@"null"]) {
                return  @"";
            }
        }
        return value;
    }
    
}

-(void)setSafeObj:(id)obj forKey:(NSString*)key{
    
    id dic=self;
    if (![self isKindOfClass:[NSDictionary class]]) {
        
        if (![self respondsToSelector:NSSelectorFromString(key)]) {
            
            [self setAddValue:obj forKey:key];
            
            return ;
        }
        
        [self setValue:obj forKey:key];
        return;
    }else{
        
        [dic setObject:obj forKey:key];
        return;
    }
    
    
    
    
    
}

-(void)setAddValue:(id)someThing forKey:(NSString *)key{
    
    if ([self isKindOfClass:[NSMutableDictionary class]]) {
        [(NSMutableDictionary*)self setObject:someThing forKey:key];
        return;
    }
    
    
    
    NSMutableDictionary *operations = objc_getAssociatedObject(self, &somethingAdd);
    if (operations) {
        //        return operations;
    }else{
        operations = [NSMutableDictionary dictionary];
        objc_setAssociatedObject(self, &somethingAdd, operations, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        //        return operations;
    }
    
    [operations setObject:someThing forKey:key];
}
-(id)getAddValueForKey:(NSString *)key{
    if ([self isKindOfClass:[NSMutableDictionary class]]) {
       return [(NSMutableDictionary*)self objectForKey:key];
    }
    
    
    NSMutableDictionary *operations = objc_getAssociatedObject(self, &somethingAdd);
    
    if (operations==nil) {
        return nil;
    }
    
    return [operations objectForKey:key];
}
-(void)removeAddValueForkey:(NSString *)key{
    if ([self isKindOfClass:[NSMutableDictionary class]]) {
        [(NSMutableDictionary*)self removeObjectForKey:key];
        return;
    }
    
    NSMutableDictionary *operations = objc_getAssociatedObject(self, &somethingAdd);
    
    if (operations==nil) {
        return ;
    }
    
    [operations removeObjectForKey:key];
    
}


-(NSString *)jsonStrSYS{
    NSError *parseError = nil;
    NSDictionary*dic=[self getpuredic];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
#pragma mark  wd 打印字符串注释
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //    return [[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
}

-(NSDictionary*)getpuredic{
    
    if ([self isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary*msdic=[NSMutableDictionary new];
        //    [mstr appendString:@"{\n"];
        NSArray*keyArry=[(NSDictionary*)self allKeys];
        for (NSString *keyname in keyArry) {
            NSObject*obj=[(NSDictionary*)self objectForJSONKey:keyname];
            obj= [(NSDictionary*)obj getpuredic];
            if (obj) {
                
                [msdic setValue:obj forKey:keyname];
            }
            
        }
        return msdic;
    }
    if ([self isKindOfClass:[NSString class]]) {
        return (NSDictionary*)self;
    }
    if ([self isKindOfClass:[NSNumber class]]) {
        return (NSDictionary*)self;
    }
    if ([self isKindOfClass:[NSArray class]]) {
        
        NSMutableArray*array=[NSMutableArray new];
        for (NSDictionary*dic in (NSArray*)self) {
            [array addObject:[dic getpuredic]];
        }
        return (NSDictionary*)array;
    }
    
    //    return self.wkeyValues;
    return [NSDictionary new];
    
}
@end
